package cn.iocoder.idata.module.idata.controller.admin.subtask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.subtask.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.subtask.SubtaskDO;
import cn.iocoder.idata.module.idata.convert.subtask.SubtaskConvert;
import cn.iocoder.idata.module.idata.service.subtask.SubtaskService;

@Tag(name = "管理后台 - 批量子任务")
@RestController
@RequestMapping("/idata/subtask")
@Validated
public class SubtaskController {

    @Resource
    private SubtaskService subtaskService;

    @PostMapping("/create")
    @Operation(summary = "创建批量子任务")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:create')")
    public CommonResult<Long> createSubtask(@Valid @RequestBody SubtaskCreateReqVO createReqVO) {
        return success(subtaskService.createSubtask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新批量子任务")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:update')")
    public CommonResult<Boolean> updateSubtask(@Valid @RequestBody SubtaskUpdateReqVO updateReqVO) {
        subtaskService.updateSubtask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除批量子任务")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('idata:subtask:delete')")
    public CommonResult<Boolean> deleteSubtask(@RequestParam("id") Long id) {
        subtaskService.deleteSubtask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得批量子任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:query')")
    public CommonResult<SubtaskRespVO> getSubtask(@RequestParam("id") Long id) {
        SubtaskDO subtask = subtaskService.getSubtask(id);
        return success(SubtaskConvert.INSTANCE.convert(subtask));
    }

    @GetMapping("/list")
    @Operation(summary = "获得批量子任务列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:query')")
    public CommonResult<List<SubtaskRespVO>> getSubtaskList(@RequestParam("ids") Collection<Long> ids) {
        List<SubtaskDO> list = subtaskService.getSubtaskList(ids);
        return success(SubtaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得批量子任务分页")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:query')")
    public CommonResult<PageResult<SubtaskRespVO>> getSubtaskPage(@Valid SubtaskPageReqVO pageVO) {
        PageResult<SubtaskDO> pageResult = subtaskService.getSubtaskPage(pageVO);
        return success(SubtaskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出批量子任务 Excel")
    // @PreAuthorize("@ss.hasPermission('idata:subtask:export')")
    @OperateLog(type = EXPORT)
    public void exportSubtaskExcel(@Valid SubtaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SubtaskDO> list = subtaskService.getSubtaskList(exportReqVO);
        // 导出 Excel
        List<SubtaskExcelVO> datas = SubtaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "批量子任务.xls", "数据", SubtaskExcelVO.class, datas);
    }

}
